﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SharpLite.Domain.DataInterfaces;
using DAEProde.Domain;

namespace DAEProde.Tests.Fake
{
    public class FakeMatchRepository : IRepository<Match>
    {
        public List<Match> partidos;
        public FakeTeamRepository teamRepository;
       // public FakeMatchDateRepository matchDateRepository;

        public FakeMatchRepository()
        {
            teamRepository = new FakeTeamRepository();
            MatchDate matchDate = new MatchDate();

            partidos = new List<Match> { 
                            null, //id = 0
                            new Match { Name = "P1", MatchDate = matchDate, LocalTeam = teamRepository.Get(1), OutTeam = teamRepository.Get(2) }, //id = 1
                            new Match { Name = "P2", MatchDate = matchDate, LocalTeam = teamRepository.Get(3), OutTeam = teamRepository.Get(4) }  //id = 2
                            };
        }

        public IDbContext DbContext
        {
            get { throw new NotImplementedException(); }
        }

        public void Delete(Match entity)
        {
            partidos.Remove(entity);
        }

        public Match Get(int id)
        {
            if (partidos.Count - 1 >= id) //el primer elemento null no se cuenta (id = 0)
            {
                return partidos.ElementAt(id);
            }
            else
            {
                return null;
            }
        }

        public IQueryable<Match> GetAll()
        {
            return partidos.AsQueryable<Match>();
        }

        public Match SaveOrUpdate(Match entity)
        {
            if (partidos.Exists(t => t == entity))
            {
                var toUpdate = partidos.Single(t => t == entity);
                return entity;
            }
            else
            {
                partidos.Add(entity);
                return entity;
            }
        }
    }
}

